<?php
error_reporting(0);
$isadmin=1; 
include_once '../../comm/config.php';
include_once '../include/page.class.php';
include_once '../../comm/checkpostandget.php';

$uploaddir = "../images/shoptype/";
if(isset($_POST['submit']) && $_POST['type'] == "add"){
	$types = array("jpg","gif","bmp","jpeg","png");
	$type = substr(strrchr($_FILES['icon']['name'],'.'),1);
	if(!in_array(strtolower($type), $types)){
		$text=implode('.',$types);
		echo "您只能上传以下类型文件: ",$text,"<br>";die;
	}else{
		$filename=explode(".",$_FILES['icon']['name']);
		$time=date("m-d-H-i-s");
		$filename[0]=$time;
		$name=implode(".",$filename);
		$uploadfile=$uploaddir.$name;
	}
	move_uploaded_file($_FILES['icon']['tmp_name'],$uploadfile);
	$sql = "INSERT INTO  ".$BIAOTOU."shoptype (`icon`,`typename`,`description`,`conditions`,`license`) VALUES ('{$name}','{$_POST["typename"]}','{$_POST["description"]}','{$_POST["conditions"]}','{$_POST["license"]}')";
	mysql_query($sql);
}elseif(isset($_POST['submit']) && $_POST['type'] == "change"){
	if(!empty($_FILES['icon']['name'])){
		$types = array("jpg","gif","bmp","jpeg","png");
		$type = substr(strrchr($_FILES['icon']['name'],'.'),1);
		if(!in_array(strtolower($type), $types)){
			$text=implode('.',$types);
			echo "您只能上传以下类型文件: ",$text,"<br>";die;
		}else{
			$filename=explode(".",$_FILES['icon']['name']);
			$time=date("m-d-H-i-s");
			$filename[0]=$time;
			$name=implode(".",$filename);
			$uploadfile=$uploaddir.$name;
		}
		move_uploaded_file($_FILES['icon']['tmp_name'],$uploadfile);
	}else{
		$uploadfile  = $_POST['iconOral'];
	}
	$sql = "UPDATE ".$BIAOTOU."shoptype SET `icon`='{$uploadfile}',`typename`='{$_POST['typename']}',`description`='{$_POST['description']}',`conditions`='{$_POST['conditions']}',`license`='{$_POST['license']}' WHERE `id`='{$_POST['id']}'";
	mysql_query($sql);
}elseif($_GET['type'] == "del"){
	$shoptype_delete = "DELETE FROM ".$BIAOTOU."shoptype WHERE `id` = {$_GET['id']}";
	mysql_query($shoptype_delete);
}
$shoptype_select ="select * from ".$BIAOTOU."shoptype";
$shoptype_selectRes = mysql_query($shoptype_select);
$shoptype_count = mysql_num_rows($shoptype_selectRes);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="../js/jquery.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>管理中心</title>
<link href="../css/style.css" rel="stylesheet" type="text/css"></head>

<body topmargin="5" leftmargin="0">
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="282" valign="top"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td>
          <table cellspacing="0" width="100%" height="207" style="border:1px  solid #DCEAF7;">
            <tr>
            <td width="300" height="30" bgcolor="E9F2FB" class="bigtext">&nbsp;<img src="../images/arrow.gif" width="16" height="22" align="absmiddle" /> <a href="certificate.php">商户认证管理</a>&nbsp;&nbsp;&nbsp;&nbsp;<strong>认证类别管理</strong></td> 
            <td width="580" height="30" bgcolor="E9F2FB" class="bigtext"></td>
            <form id="form1" name="form1" method="get" action="">
		          <td width="300" align="right" bgcolor="E9F2FB" class="bigtext">
		              <label>
		              	<input type="button" name="add" value="添加类别" onclick="location.href='authtype.php?type=add'">
		              </label>
	              	共有 <b><?php echo $shoptype_count;?></b> 条记录&nbsp;&nbsp;
	              </td>
              </form>
            </tr>
            <?php 
            	if($_GET['type'] == 'add'){
            ?>
            	<tr>
	              <td colspan="3" valign="top"><table width="100%" height="300" border="0" cellpadding="0" cellspacing="0">
	                <tr>
		                <td valign="top">
			                <form method="POST" action="authtype.php" enctype="multipart/form-data">
			                <input type="hidden" name="type" value="add">
			                	<table width="100%" border=1 cellpadding=0 cellspacing=0	style="border-collapse: collapse" bordercolor="#dddddd">
			                    <tr>
		                        	<td height="30" align="right" class="bigtext">认证名称：</td>
		                        	<td height="30">&nbsp;
		                        	<input type="text" name="typename"  value=""/>
		                        	</td>
		                        </tr>
		                        <tr>
		                        	<td height="30" align="right" class="bigtext">认证图标：</td>
		                        	<td height="30">&nbsp;
		                        	<input type="file" id="pic" name="icon" onchange="PreviewImage('Preview',this,56,56);" />
		                        	 <div id="Preview" style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);"></div>
		                        	</td>
		                        </tr>
		                        <tr>
		                        	<td height="30" align="right" class="bigtext">说明：</td>
		                        	<td height="30">&nbsp;
		                        	<input type="text" name="description"  value=""/>
		                        	</td>
		                        </tr>
		                        <tr>
		                        	<td height="30" align="right" class="bigtext"><input type="checkbox" id="checkconditions" >补充条件：</td>
		                        	<td height="30" id="conditions_wrap">
		                        		
		                        	</td>
		                        </tr>
		                        <tr>
		                        	<td height="30" align="right" class="bigtext"><input type="checkbox" id="checklicense" >是否上传图片：</td>
		                        	<td height="30" id="license_wrap">
		                        	</td>
		                        </tr>
		                        <tr>
		                        	<td></td>
		                        	<td><input type="submit" name="submit" value="提交"></td>
		                        </tr>
		                       </table>
			                </form>
						</td>
	                </tr>
	              </table></td>
	            </tr>
            <?php 
            	}elseif($_GET['type'] == 'change'){
            		$shoptype_change ="select * from ".$BIAOTOU."shoptype where id={$_GET['id']}";
            		$shoptype_changeRes = mysql_query($shoptype_change);
            		$shoptype_row = mysql_fetch_array($shoptype_changeRes);
            ?>
            	 <tr>
	              <td colspan="3" valign="top"><table width="100%" height="300" border="0" cellpadding="0" cellspacing="0">
	                <tr>
	                  <td valign="top">
	                  <form method="POST" action="authtype.php" enctype="multipart/form-data">
		                  <table width="100%" border=1 cellpadding=0 cellspacing=0	style="border-collapse: collapse" bordercolor="#dddddd">
		                  <input type="hidden" name="type" value="change">
		                  <input type="hidden" name="id" value="<?php echo $shoptype_row['id'];?>">
		                    <tr>
	                        	<td height="30" align="right" class="bigtext">认证名称：</td>
	                        	<td height="30">&nbsp;
	                        	<input type="text" name="typename"  value="<?php echo $shoptype_row['typename'];?>"/>
	                        	</td>
	                        </tr>
	                        <tr>
	                        	<td height="30" align="right" class="bigtext">认证图标：</td>
	                        	<td height="30">&nbsp;
	                        	<input type="hidden" name="iconOral" value="<?php echo $shoptype_row['icon'];?>">
	                        	<input type="file" id="pic" name="icon" onchange="PreviewImage('Preview',this,56,56);"/>
			                        	 <div id="Preview" style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);"><img src="<?php echo $uploaddir.$shoptype_row['icon'];?>"></div>
	                        	</td>
	                        </tr>
	                        <tr>
	                        	<td height="30" align="right" class="bigtext">说明：</td>
	                        	<td height="30">&nbsp;
	                        	<input type="text" name="description"  value="<?php echo $shoptype_row['description'];?>"/>
	                        	</td>
	                        </tr>
	                        <tr>
	                        	<td height="30" align="right" class="bigtext">补充条件：</td>
	                        	<td height="30">&nbsp;
	                        	<input type="text" name="conditions"  value="<?php echo $shoptype_row['conditions'];?>"/>
	                        	</td>
	                        </tr>
	                        <tr>
	                        	<td height="30" align="right" class="bigtext">是否上传图片：</td>
	                        	<td height="30">&nbsp;
	                        	<input type="text" name="license"  value="<?php echo $shoptype_row['license'];?>"/>
	                        	</td>
	                        </tr>
	                        <tr>
	                        	<td></td>
	                        	<td><input type="submit" name="submit" value="提交"></td>
	                        </tr>
	                       </table>
                       </form>
						</td>
	                </tr>
	              </table></td>
	            </tr>
            <?php 
            	}else{
            ?>
            	 <tr>
              <td colspan="3" valign="top"><table width="100%" height="300" border="0" cellpadding="0" cellspacing="0">
                <tr>
                  <td valign="top"><table width="100%" border=1 cellpadding=0 cellspacing=0
style="border-collapse: collapse" bordercolor="#dddddd">
                  
                    <tr>
                      <td width="4%" height="30" align="center" bgcolor="#F2F2F2" class="bigtext"><strong>&nbsp;序列</strong></td>
                      <td width="20%" height="30" align="center" bgcolor="#F2F2F2" class="bigtext"><strong>&nbsp;图标</strong></td>
                      <td width="20%" align="center" bgcolor="#F2F2F2" class="bigtext"><strong>名称</strong></td>
                      <td width="13%" align="center" bgcolor="#F2F2F2" class="bigtext"><strong>&nbsp;说明</strong></td>
                      <td width="11%" align="center" bgcolor="#F2F2F2" class="bigtext"><strong>&nbsp;条件提示</strong></td>
                      <td width="7%" align="center" bgcolor="#F2F2F2" class="bigtext"><strong>&nbsp;是否上传图片</strong></td>
                      <td width="12%" align="center" bgcolor="#F2F2F2" class="bigtext"><strong>&nbsp;操作</strong></td>
                    </tr>
					<?php
					$i = 1;
					while ($row = mysql_fetch_array($shoptype_selectRes))
					{
						echo "<tr>";
                      	echo "<td align=center height=30 class=bigtext>&nbsp;".$i."</td>";
                      	echo "<td align=center height=30 class=bigtext>&nbsp;<img src='".$uploaddir.$row["icon"]."'/></td>";
                      	echo "<td align=center height=30 class=bigtext>&nbsp;".$row["typename"]."</td>";
						echo "<td align=center class=bigtext>&nbsp;".$row["description"]."</td>";
						echo "<td align=center class=bigtext>&nbsp;".$row["conditions"]."</td>";
						if(empty($row["license"])){
							$license = '否';
						}else{
							$license = '是';
						}
						echo "<td align=center class=bigtext>&nbsp;".$license."</td>";
						echo "<td align=center align=center class=bigtext><a href='authtype.php?id=".$row["id"]."&type=change' class=link4>修改</a>&nbsp;<a href='javascript:void(0);' onclick=Delete('{$row['typename']}','{$row['id']}') class=link4>删除</a></td>";
						echo "</tr>";
						$i ++;
					}
					?></table>
					<div class="yahoo2" style=" margin-top:30px;"><?php 
				pageft($total,$pagesize,1,1,0,10,$page_url);
				echo $pagenav;?></div>
					</td>
                </tr>
              </table></td>
            </tr>
            <?php 
            	}
            ?>
          </table></td>
        </tr>
      
    </table></td>
  </tr>
</table>
<script>
$(function(){
	$("#checkconditions").bind("click",function(){
		if($(this).attr("checked") == true){
			$("#conditions_wrap").append("&nbsp;<input type='text' id='conditions' name='conditions'  value=''/>");
		}else{
			$("#conditions_wrap").empty();
		}
	});

	$("#checklicense").bind("click",function(){
		if($(this).attr("checked") == true){
			$("#license_wrap").append("&nbsp;<input type='text' id='license' name='license'  value=''/>");
		}else{
			$("#license_wrap").empty();
		}
	});
});
function allowConfirm(shopname,type,id){
	 if(confirm("通过'"+shopname+"'的'"+type+"'申请？")){
		 alert("已经通过'"+shopname+"'的'"+type+"'申请");
		 location.href="./certificate.php?link=verfying&action=allow&id="+id;
	 }else{
		 return false;
	 }
	    
}
function checkPic() {
	var picPath = document.getElementById("pic").value;
	var type = picPath.substring(picPath.lastIndexOf(".") + 1, picPath.length).toLowerCase();
	if (type != "jpg" && type != "bmp" && type != "gif" && type != "png") {
		alert("请上传正确的图片格式");
		return false;
	}
	return true;
}
//图片预览
function PreviewImage(divImage, upload, width, height) {
	if (checkPic()) {
		try {
			var imgPath;      //图片路径
			var Browser_Agent = navigator.userAgent;//判断浏览器的类型
				if (Browser_Agent.indexOf("Firefox") != -1) {//火狐浏览器
				document.getElementById(divImage).innerHTML ="";
				document.getElementById(divImage).innerHTML = "<img id='imgPreview'  width='" + width + "' height='" + height + "'/>";
				var imgObjPreview=document.getElementById("imgPreview");
				imgObjPreview.src = window.URL.createObjectURL(upload.files[0]);
			} else { //IE浏览器 ie9 必须在兼容模式下才能显示预览图片
				var Preview = document.getElementById(divImage);
				document.getElementById(divImage).innerHTML ="";
				Preview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = upload.value;
				Preview.style.width = width;
				Preview.style.height = height;
			}
		} catch (e) {
			alert("请上传正确的图片格式");
		}
	}
}


function Delete(typename,id){
	if(confirm("您确定删除'"+typename+"'么？")){
		 location.href="authtype.php?type=del&id="+id;
	 }else{
		 return false;
	 }
}
</script>
</body>
</html>
<?php mysql_close($link);?>
